package jwebtester.business.beans.competencies;

import infrastructure.Scopes;
import jwebtester.business.utils.Dialogs;
import jwebtester.business.utils.DialogsHelper;
import jwebtester.business.utils.FacesHelper;
import jwebtester.dal.provider.CompetenciesDataProvider;

import org.primefaces.event.SelectEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import domain.entities.Competency;

@Component
@Scope(Scopes.Request)
public class EditCompetencyBean {

	private static final Logger LOG = LoggerFactory.getLogger(EditCompetencyBean.class);

	@Autowired
	private transient CompetenciesDataProvider dataProvider;

	private transient Competency parentCompetency;

	private transient Competency editCompetency;

	public EditCompetencyBean() {
		editCompetency = new Competency();
		parentCompetency = new Competency();
	}

	public Competency getEditCompetency() {
		return editCompetency;
	}

	public Competency getParentCompetency() {
		return parentCompetency;
	}

	public void setParentCompetency(final Competency parentCompetency) {
		this.parentCompetency = parentCompetency;
	}

	public void setEditCompetency(final Competency editCompetency) {
		this.editCompetency = editCompetency;
	}

	public void beforeUpdateCompetency(final Competency competency) {
		final int competencyID = competency.getId();
		try {
			// refresh competency before edit; it might be edited
			editCompetency = dataProvider.getCompetency(competencyID);

			if (editCompetency.getParentID() != null) {
				parentCompetency = dataProvider.getCompetency(editCompetency.getParentID());
			}

			new DialogsHelper().showDialog(Dialogs.EDIT_COMPETENCY);
		} catch (RuntimeException e) {
			LOG.error(String.format("Error refreshing competency id=%s before edit", competencyID));
		}
	}

	public void handleSelectParent(final SelectEvent event) {
		final Object selectedObject = event.getObject();
		if (selectedObject != null) {
			parentCompetency = (Competency) selectedObject;
		}
	}

	public void updateCompetency() {
		try {

			if (parentCompetency != null && parentCompetency.getId() == editCompetency.getId()) {
				notifyParentSelf();
			} else {
				if (parentCompetency != null && !parentCompetency.isNew()) {
					editCompetency.setParentID(parentCompetency.getId());
				}

				dataProvider.updateCompetency(editCompetency);
				notifySuccess();
			}
		} catch (Exception e) {
			notifyError(e);
		}
	}

	private void notifySuccess() {
		new FacesHelper().info("Success", "Competency has been successfully updated");
	}

	private void notifyError(final Exception error) {
		LOG.error(String.format("Error updating competency"), error);
		new FacesHelper().error("Error", "Error updating competency");
	}

	private void notifyParentSelf() {
		new FacesHelper().error("Error", "Can't set parent competency to self. Please choose another one parent");
	}
}
